<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/28 0028
 * Time: 下午 15:27
 */

namespace Background\Controller;


use Model\GoodsModel;
use Think\Controller;


class GoodsController extends Controller
{
    /**
     *
     */
    public function show()
    {
        $goods = new GoodsModel();

//        $info = $goods->order("goods_id desc")->select();

//        $this->assign('info', $info);
//        $this->display();

        $goods = new GoodsModel();

        $nowinfo = $goods->fetchData();

        $info = $nowinfo['pageinfo']; //当前页数据信息
        $pagelist = $nowinfo['pagelist'];//页码列表信息

        $this->assign('info', $info);    //填充数据到布局里面name为info的表单里面。
        $this->assign('pagelist', $pagelist);//填充数据到布局里面name为pagelist的表单里面。
        $this->display(); //展示。
    }

    public function add()
    {
//        $good = D('goods');
        $goods = new GoodsModel();

        if (IS_POST) {
            $data = $goods->create();
            //对富文本编辑器原生内容进行过滤，方式xss攻击
            //htmlpurifier过滤
            $data['goods_introduce'] = \fanXSS($_POST['goods_introduce']);

            if ($goods->add($data)) {
                $this->success('添加商品成功', U('show'));
            } else {
                $this->error('添加商品失败', U('add'));
            }
//            dump($_POST);
        } else {
            $this->display();
        }

    }

    public function update()
    {
        $goods = new GoodsModel();
        if (IS_POST) {
            $data = $goods->create();
            if ($goods->save($data))
                $this->success('修改商品成功', U('show'), 2);
            else
                $this->error('修改商品失败', U('update', array('goods_id' => $data['goods_id'])), 2);
        } else {
            $goods_id = I('get.goods_id'); //接收被修改商品的goods_id
            $info = $goods->find($goods_id);//查询被修改商品的信息

//            获取相册信息
            $picInfo = D('GoodsPics')->where(array("goods_id" => $goods_id))->select();
            if (!empty($picInfo)) {
                $this->assign('picsinfo', $picInfo);
            }
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function delPics()
    {
        $pics_id = I('get.pics_id');
        //查询图片并
        $info = D('GoodsPics')->find($pics_id);
        //①删除相册图片[物理删除]
        unlink($info['pics_big']);
        unlink($info['pics_small']);

        //②删除数据记录信息
        $z = D('GoodsPics')->delete($pics_id); //返回删除记录条数,1条
        if ($z) {
            echo "删除成功！";
        }
    }


}